package com.ellricky.baselib.util

import android.app.Activity
import android.text.TextUtils
import com.andr.common.http.dialog.ProgressDialog
import com.andr.common.http.util.JsonUtil
import com.andr.common.util.ToastUtil

import com.zhy.http.okhttp.callback.StringCallback

import okhttp3.Call
import okhttp3.Request
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject

abstract class NewEBaseCallback : StringCallback {

    var showLoading = false
    var mContext: Activity? = null
    var mDialog: ProgressDialog? = null
    var loadMsg: String? = null
    var canCancel = false

    constructor(context: Activity?) : this(context, true)

    constructor(context: Activity?, showLoading: Boolean) {
        canCancel = true
        this.showLoading = showLoading
        mContext = context
    }

    constructor(context: Activity?, showLoading: Boolean, loadMsg: String?) {
        canCancel = true
        this.showLoading = showLoading
        mContext = context
        this.loadMsg = loadMsg
    }

    fun setDialogCancel(cancel: Boolean) {
        canCancel = cancel
    }

    override fun onBefore(request: Request?, id: Int) {
        super.onBefore(request, id)
        showDialog()
    }

    override fun onError(call: Call?, e: Exception?, id: Int) {
        var msg: String? = "未知错误"
        if (e != null) {
            val message = e.message
            if (!TextUtils.isEmpty(message)) {
                msg = message
            }
        }
        onHttpError("-1000", msg)
    }

    override fun onAfter(id: Int) {
        super.onAfter(id)
        dismissDialog()
    }

    private fun showDialog() {
        if (showLoading && mContext != null && !mContext!!.isFinishing) {
            if (mDialog == null) {
                mDialog = if (TextUtils.isEmpty(loadMsg)) {
                    ProgressDialog(mContext)
                } else {
                    ProgressDialog(mContext, loadMsg)
                }
                mDialog!!.setCancelable(canCancel)
                mDialog!!.setCanceledOnTouchOutside(canCancel)
            }
            if (!mDialog!!.isShowing) {
                mDialog!!.show()
            }
        }
    }

    private fun dismissDialog() {
        if (mDialog != null && mDialog!!.isShowing) {
            mDialog!!.dismiss()
        }
    }

    override fun onResponse(response: String?, id: Int) {
        try {
            val resultJ0 = JSONObject(response!!)
            val code = resultJ0.getString("msgcode")
            val msg = resultJ0.getString("msg")
            val data = resultJ0.optString("data")
            if ("1" == code) {
                when (dataFlag()) {
                    1 -> {
                        val dataJO = JsonUtil.parseJO(resultJ0.toString())
                        parseDataJO(code, msg, dataJO)
                    }
                    2 -> {
                        val dataJA = JsonUtil.parseJA(resultJ0.toString())
                        parseDataJA(code, msg, dataJA)
                    }
                    else -> {
                        parseDataStr(code, msg, data.toString())
                    }
                }
            } else if ("0" == code) {
                val dataJO = JsonUtil.parseJO(resultJ0.toString())
                parseDataJO(code, msg, dataJO)
            }
        } catch (var12: JSONException) {
            onError(null as Call?, var12, -1)
        }
    }

    fun dealTimeStamp(retCode: String?, timeStamp: String?) {}

    abstract fun dataFlag(): Int

    open fun onHttpError(code: String?, msg: String?) {
        ToastUtil.show(mContext, msg)
    }

    open fun parseDataJO(code: String?, msg: String?, dataJO: JSONObject?) {}

    open fun parseDataJA(code: String?, msg: String?, dataJA: JSONArray?) {}

    open fun parseDataStr(code: String?, msg: String?, dataStr: String?) {}
}